package com.shaozi.nc.mq.substance;

import java.lang.reflect.Type;
import java.util.List;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.reflect.TypeToken;
import com.shaozi.data.mq.business.base.SourceTypeConsts.OA.Report;
import com.shaozi.data.mq.business.bean.MQExchangeRuleBean;
import com.shaozi.data.mq.business.bean.MQPushItem;
import com.shaozi.data.mq.business.bus.substance.BasePushSubstanceHandler;
import com.shaozi.data.mq.business.utils.MqUtil;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.mq.bean.base.BizBaseBean;
import com.shaozi.nc.mq.bean.report.AlarmByOtherBean;
import com.shaozi.nc.mq.bean.report.AlarmByOtherUserItemBean;
import com.shaozi.nc.mq.bean.report.RecvNewBean;
import com.shaozi.nc.mq.bean.report.ReportCommentBean;
import com.shaozi.nc.mq.bean.report.RuleUpdateBean;
import com.shaozi.nc.mq.bean.report.TemplateUpdateBean;
import com.shaozi.utils.common.JsonUtil;

public class ReportPushSubstance extends BasePushSubstanceHandler {
	private final Logger logger = Logger.getLogger(ReportPushSubstance.class);

	public ReportPushSubstance() {

	}

	@Override
	protected void onExchangeBiz(MQExchangeRuleBean pushData, MQPushItem pushItem, JSONObject jsonObj)
			throws SzException {
		switch (pushItem.getSourceType()) {
		case Report.AlarmByOther:
			exchangeAlarmByOther(pushData, pushItem, jsonObj);
			break;
		case Report.Recv_New:
			exchangeBiz(pushData, jsonObj, RecvNewBean.class);
			break;
		case Report.Rule_Update:
			exchangeBiz(pushData, jsonObj, RuleUpdateBean.class);
			break;
		case Report.Comment:
		case Report.Reply_Comment:
			exchangeBiz(pushData, jsonObj, ReportCommentBean.class);
			break;
		case Report.Rule_Look_Report:
			exchangeBiz(pushData, jsonObj, BizBaseBean.class);
			break;
		case Report.Template_Update:
			exchangeBiz(pushData, jsonObj, TemplateUpdateBean.class);
			break;
		default:
			pushData.setBiz(null);
			break;
		}
	}

	private void exchangeAlarmByOther(MQExchangeRuleBean pushData, MQPushItem pushItem, JSONObject jsonObj)
			throws JSONException, SzException {
		Type type = new TypeToken<List<AlarmByOtherUserItemBean>>() {
		}.getType();
		List<AlarmByOtherUserItemBean> items = JsonUtil
				.toObject(MqUtil.getBizJson(jsonObj).getJSONArray("toUsers").toString(), type);
		AlarmByOtherBean t = JsonUtil.toObject(MqUtil.getBizString(jsonObj), AlarmByOtherBean.class);
		t.setToUsers(items);
		pushData.setBiz(t);
	}
}
